package com.cad3d.filemanager.dbutils;

import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import com.cad3d.filemanager.dao.*;

public class FileManagementTableTool {
	Logger logger = Logger.getLogger(FileManagementTableTool.class);
	public static FileManagementTableTool getInstance() {
		FileManagementTableTool roomsTableTool = new FileManagementTableTool();
//		roomsTableTool.sessionFactory = HibernateUtil.getSessionFactory();
		return roomsTableTool;
	}

	public List<Filemanagement> getAllFiles() {
		List<Filemanagement> list = new ArrayList<Filemanagement>();
		logger.debug("get all files");
		try {
			//Session session = sessionFactory.openSession();
			Session session = HibernateUtil.getSession();
			list = session.createCriteria(Filemanagement.class)
					.addOrder(Order.desc("fileId"))
					.list();
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.error(ex.getMessage());
		}
		return list;
	}

	public List<Filemanagement> getAllFilesPublic(User user) {
		List<Filemanagement> list = new ArrayList<Filemanagement>();
		logger.debug("get all files Public");
		try {
			//Session session = sessionFactory.openSession();
			Session session = HibernateUtil.getSession();
			list = session.createCriteria(Filemanagement.class)
					.addOrder(Order.desc("fileId"))
					.add(Restrictions.or(
							Restrictions.eq("filePublic", true),
							Restrictions.eq("user",user )
							))
					.list();
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.error(ex.getMessage());
		}
		return list;
	}
	
	public Filemanagement findFileById(int fileId) {
		logger.debug("find file by id");
		Filemanagement file = null;
		try {
//			Session session = sessionFactory.openSession();
			Session session = HibernateUtil.getSession();
			file = (Filemanagement) session
					.createCriteria(Filemanagement.class)
					.add(Restrictions.eq("fileId", fileId))
					.addOrder(Order.desc("fileId"))
					.uniqueResult();
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.error(ex.getMessage());
		}
		return file;
	}

	public List<Filemanagement> findFileByFilename(String fileName) {
		logger.debug("find file by file name");
		List<Filemanagement> list = new ArrayList<Filemanagement>();
		try {
//			Session session = sessionFactory.openSession();
			Session session = HibernateUtil.getSession();
			list = session.createCriteria(Filemanagement.class)
					.add(Restrictions.like("fileName", "%" + fileName + "%"))
					.addOrder(Order.desc("fileId"))
					.list();
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.error(ex.getMessage());
		}
		return list;
	}
	
	public List<Filemanagement> findFileByFullFilename(String fileName) {
		logger.debug("find file by full file name");
		List<Filemanagement> list = new ArrayList<Filemanagement>();
		try {
//			Session session = sessionFactory.openSession();
			Session session = HibernateUtil.getSession();
			list = session.createCriteria(Filemanagement.class)
					.add(Restrictions.like("fileName",fileName))
					.addOrder(Order.desc("fileId"))
					.list();
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.error(ex.getMessage());
		}
		return list;
	}
	
	public boolean addFile(Filemanagement file) {
		logger.debug("add file");
		Session session = HibernateUtil.getSession();
		Transaction trans = session.beginTransaction();
		try {	
			session.save(file);
			trans.commit();
			//close session after add a record to avoid Hibernate Lazy Exception
			session.close();
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	public List<Filemanagement> findFileByExtension(String fileType) {
		logger.debug("find file by extension");
		List<Filemanagement> list = new ArrayList<Filemanagement>();
		try {
//			Session session = sessionFactory.openSession();
			Session session = HibernateUtil.getSession();
			list = session.createCriteria(Filemanagement.class)
					.add(Restrictions.like("fileType", "%" + fileType + "%"))
					.addOrder(Order.desc("fileId"))
					.list();
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.error(ex.getMessage());
		}
		return list;
	}


	public boolean editFileInfo(Filemanagement file) {

		logger.debug("Edit file info");
//		Session session = sessionFactory.openSession();
		Session session = HibernateUtil.getSession();
		Transaction trans = session.beginTransaction();
		try {
			session.saveOrUpdate(file);
			trans.commit();
		} catch (Exception ex) {
			trans.rollback();
			ex.printStackTrace();
			logger.error(ex.getMessage());
			return false;
		}
		return true;
	}

	public boolean deleteFile(Filemanagement file) {

//		Session session = sessionFactory.openSession();

		logger.debug("delete file: "+ file.getFileName());
		Session session = HibernateUtil.getSession();
		Transaction trans = session.beginTransaction();
		try {
			session.delete(file);
			trans.commit();
		} catch (Exception ex) {
			trans.rollback();
			logger.error(ex.getMessage());
			ex.printStackTrace();
			return false;
		}
		return true;
	}
}
